<script setup>
// 获取父组件的文章信息
import {onBeforeMount, onMounted, toRefs} from "vue";
import router from "@/router/index.js";

const props = defineProps({
  article: {
    type: Object,
    default: {
      id: 0,
      title: '',
      createTime: ''
    }
  },
  user: {
    type: Object,
    default: {
      id: 0,
      username: '',
    }
  },
  knowledgeBases: {
    type: Array,
    default: []
  }
})

const {article, user, knowledgeBases} = toRefs(props)

console.log(user.id)



const navigateToArticleDetail = () => {
  // 跳转到文章详情页
  console.log("跳转到文章详情页, 文章id => ", article.value.id)
  router.push({
    name: "article-detail",
    params: {
      id: article.value.id
    }
  })
}
</script>

<template>
  <!-- 文章卡片，包含文章标题、作者名称、发布时间、知识库标签 -->
  <div class="article-item" @click="navigateToArticleDetail">
    <!--  标题-->
    <div class="title van-ellipsis">{{ article.title }}</div>
    <div class="knowledge-base">
      <!--  知识库标签-->
      <van-tag v-for="(tag, index) in knowledgeBases" :key="index" class="tag" type="primary" plain>{{ tag.name }}</van-tag>
    </div>
    <van-divider/>
    <div class="author-and-time">
      <!--  作者-->
      <div v-if="user.id === 1" class="author-name">系统文章</div>
      <div v-else class="author-name">作者：{{ user.username }}</div>
      <!--  发布时间-->
      <div class="create-time">{{ article.updateTime }}</div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
/* 文章卡片,为长方形 */
.article-item {
  box-sizing: border-box;
  width: 100%;
  min-height: 5rem;
  padding: 10px;
  overflow: hidden;
  border-radius: .25rem;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);

  & + .article-item {
    margin-top: .5rem;
  }

  .title {
    font-weight: bold;
    font-size: 1rem;
    font-family: 'Arial', sans-serif;
  }

  .knowledge-base {
    display: flex;
    flex-wrap: wrap;
    min-height: 1.5rem;
    margin-top: .5rem;

    .van-tag {
      padding: 0 .5rem;
    }
  }

  .author-and-time {
    display: flex;
    justify-content: space-between;

    .author-name, .create-time {
      font-size: .75rem;
    }
  }

  .van-divider {
    margin: .5rem 0;
  }
}

/* 文章卡片顺序由三种颜色交替变化 */
.article-item:nth-child(3n + 1) {
  color: #00008b;
  background-color: #CCF;
}

.article-item:nth-child(3n + 2) {
  color: #8b0000;
  background-color: #FCC;
}

.article-item:nth-child(3n + 3) {
  color: #008b00;
  background-color: #CFF;
}
</style>